package com.bnz.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.bnz.manager.entity.ItemEntity;
import com.bnz.manager.service.ItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.manager.dao.GoodsDao;
import com.bnz.manager.entity.GoodsEntity;
import com.bnz.manager.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private ItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    //商品审核
    @Override
    public void updateStatus(String id, String status) {
        //先查询出对象
        GoodsEntity byId = this.getById(id);
        //修改状态
        byId.setAuditStatus(status);
        //修改
        this.updateById(byId);

        //
        //第二部分：查询得到sku商品列表
        List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id).eq("status",1));

        //第三部分：将其转换为字符串发送出去
        rabbitTemplate.convertAndSend("rmb-updateStatus", JSON.toJSONString(items));

        System.out.println("己经将经过审核的商品信息发送出去了！");
    }

}